<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * Controller for managing sports.
 * @package backend-sports/conrollers
 * @category Controller
 * 
 * @author Mark Angelo Angulo
 * @copyright (c) 2015
 */
class Lottery extends Backend_Controller {

    /**
     *  Constructor method.
     */
    function __construct()
    {
        parent::__construct();
        
        $this->load->helper('cms');
        $this->load->helper('file');
        
        $this->load->library('form_validation');
        $this->form_validation->set_error_delimiters('<div class="clear-fix"></div><small>', '</small>');
    }

    /**
     * Default method called when nothing is specified.
     * Displays dashboard.
     * @access public
     */
    public function index()
    {
        
        $every = 6;
        
//        echo $n = 2011 / 10;
//        echo '<br>';
//        echo $n = round( (abs($n) - (int)$n) * 10 );
//        exit;
        
        $combinations = file('csvs/lottery.txt');
        $chance = array();
        
        $draw_counter = 0;
        foreach( $combinations as $combination )
        {
            $draw_counter++;
            $draw_counter = $draw_counter > $every ? 1 : $draw_counter;
            $numbers = explode( '-', trim($combination) );

            foreach( $numbers as $number )
            {
                $number = trim( $number );
                if( isset( $chance[$number][$draw_counter] ) )
                {
                    $chance[$number][$draw_counter] = (int) $chance[$number][$draw_counter] + 1;
                } else
                {
                    $chance[$number][$draw_counter] = 1;
                }
            }
        }
        
        $draw_num = ( count($combinations) + 1 ) / $every;
        $draw_num = round( (abs($draw_num) - (int)$draw_num) * $every );
        $chosen = array();
        for( $i = 1; $i < 43; $i++ )
        {
            $chosen[$i] = $chance[$i][$draw_num];
        }
        
        arsort($chosen);
        echo '<pre>';
        echo $draw_num;
        print_r($chosen); 
        echo '</pre>';
        exit();
        
        $single = array();
        $double = array();
        $triple = array();
        $quadruple = array();
        $pentuple = array();
        $sextuple = array();
        
        foreach( $combinations as $combination )
        {
            $numbers = explode( '-', trim($combination) );
//            foreach( $numbers as $number )
//            {
//                $number = trim( $number );
//                if ( isset($single[$number]) )
//                {
//                    $single[$number] = $single[$number] + 1;
//                } else
//                {
//                    $single[$number] = 1;
//                }
//                
//            }
            
            foreach( $numbers as $number )
            {
                $number = trim( $number );
                if ( isset($single[$number]) )
                {
                    $single[$number] = $single[$number] + 1;
                } else
                {
                    $single[$number] = 1;
                }
                
            }            
            
            $max = 6;
            // double
//            for( $i = 0; $i < $max; $i++ )
//            {
//                for( $j = $i+1; $j < $max; $j++ )
//                {
//                    $double_index = $numbers[$i] < $numbers[$j] ? $numbers[$i] .'-'. $numbers[$j] : $numbers[$j] .'-'. $numbers[$i];
//                    if ( isset($double[ $numbers[$i] .'-'. $numbers[$j] ]) )
//                    {
//                        $double[ $double_index ] = (int) $double[ $double_index ] + 1;
//                    } else
//                    {
//                        $double[ $double_index ] = 1;
//                    }
//                }
//            }
            
            // triple
//            for( $i = 0; $i < $max; $i++ )
//            {
//                for( $j = $i+1; $j < $max; $j++ )
//                {
//                    for( $k = $j+1; $k < $max; $k++ )
//                    {
//                        $triple_index_array = array($numbers[$i], $numbers[$j], $numbers[$k]);
//                        asort($triple_index_array);
//                        $triple_index = implode('-', $triple_index_array);
//                        
//                        if ( isset($triple[$triple_index]) )
//                        {
//                            $triple[$triple_index] = (int) $triple[$triple_index] + 1;
//                        } else
//                        {
//                            $triple[$triple_index] = 1;
//                        }
//                        
//                    }
//                }
//            }
            
            // quadruple
//            for( $i = 0; $i < $max; $i++ )
//            {
//                for( $j = $i+1; $j < $max; $j++ )
//                {
//                    for( $k = $j+1; $k < $max; $k++ )
//                    {
//                        for( $l = $k+1; $l < $max; $l++ )
//                        {
//                            
//                            $quadruple_index_array = array($numbers[$i], $numbers[$j], $numbers[$k], $numbers[$l]);
//                            asort($quadruple_index_array);
//                            $quadruple_index = implode('-', $quadruple_index_array);
//
//                            if ( isset($quadruple[$quadruple_index]) )
//                            {
//                                $quadruple[$quadruple_index] = (int) $quadruple[$quadruple_index] + 1;
//                            } else
//                            {
//                                $quadruple[$quadruple_index] = 1;
//                            }
//                            
//                        }
//                    }
//                }
//            }
            
            // pentuple
//            for( $i = 0; $i < $max; $i++ )
//            {
//                for( $j = $i+1; $j < $max; $j++ )
//                {
//                    for( $k = $j+1; $k < $max; $k++ )
//                    {
//                        for( $l = $k+1; $l < $max; $l++ )
//                        {
//                            for( $m = $l+1; $m < $max; $m++ )
//                            {
//                                $pentuple_index_array = array($numbers[$i], $numbers[$j], $numbers[$k], $numbers[$l], $numbers[$m]);
//                                asort($pentuple_index_array);
//                                $pentuple_index = implode('-', $pentuple_index_array);
//
//                                if ( isset($pentuple[$pentuple_index]) )
//                                {
//                                    $pentuple[$pentuple_index] = (int) $pentuple[$pentuple_index] + 1;
//                                } else
//                                {
//                                    $pentuple[$pentuple_index] = 1;
//                                }
//                                
//                            }
//                        }
//                    }
//                }
//            }
            
            // sextuple
//            for( $i = 0; $i < $max; $i++ )
//            {
//                for( $j = $i+1; $j < $max; $j++ )
//                {
//                    for( $k = $j+1; $k < $max; $k++ )
//                    {
//                        for( $l = $k+1; $l < $max; $l++ )
//                        {
//                            for( $m = $l+1; $m < $max; $m++ )
//                            {
//                                for( $n = $m+1; $n < $max; $n++ )
//                                {
//                                    $sextuple_index_array = array($numbers[$i], $numbers[$j], $numbers[$k], $numbers[$l], $numbers[$m], $numbers[$n]);
//                                    asort($sextuple_index_array);
//                                    $sextuple_index = implode('-', $sextuple_index_array);
//
//                                    if ( isset($sextuple[$sextuple_index]) )
//                                    {
//                                        $sextuple[$sextuple_index] = (int) $sextuple[$sextuple_index] + 1;
//                                    } else
//                                    {
//                                        $sextuple[$sextuple_index] = 1;
//                                    }
//                                }
//                            }
//                        }
//                    }
//                }
//            }
            
            
        }
//        if ( ! write_file('csvs/strong1.txt', implode("\n", $single) ))
//        {
//             echo 'Unable to write the file';
//        }
//        else
//        {
//             echo 'File written!';
//        }
        
        arsort($sextuple);
        
        echo '<pre>';
        print_r( $sextuple ); 
        echo '</pre>';
        exit();
    }
}

/* End of file sport.php */
/* Location: ./application/sports/backend-sports/controllers/sport.php */